package chris.helloWorld;

import chris.ConFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Send
 *
 * @author nanxin.chen
 * @since 2017/7/5
 */
public class Send {
    public final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory cf = ConFactory.getConnectionFactory();
        Connection con = cf.newConnection();
        Channel channel = con.createChannel();

        // durable 是否持久化;
        // exclusive 是否独占, 声明为独占的, 同时只能有一个consumer可以消费,
        // autoDelete true 最后一个consumer关闭后就会删除队列，即使队列还有未消费的数据
        channel.queueDeclare(QUEUE_NAME, false, false, true, null);
        String message = "hello world";
        for (int i = 0; i < 20; i++) {
            String sendStr = message + i;
            //we are using a default exchange "",
            //the queue name is the routing key of default exchange
            channel.basicPublish("", QUEUE_NAME, null, sendStr.getBytes());
            System.out.println(" [x] Sent '" + sendStr + "'");
        }

        channel.close();
        con.close();
    }

}
